package com.luoxiang.socialandroid.activity;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.KeyEvent;
import android.view.View;
import android.view.inputmethod.InputMethodManager;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.baidu.mapapi.search.sug.SuggestionResult;
import com.baidu.mapapi.search.sug.SuggestionSearch;
import com.baidu.mapapi.search.sug.SuggestionSearchOption;
import com.luoxiang.socialandroid.R;
import com.luoxiang.socialandroid.adapter.PositionAdapter;
import com.luoxiang.socialandroid.app.MyAppliaction;
import com.luoxiang.socialandroid.base.BaseActivity;
import com.luoxiang.socialandroid.utils.CustomClickListener;

import java.util.ArrayList;
import java.util.Collections;

public class PositionActivity extends BaseActivity {
    private TextView textComplete, tx_cancel, tx_search_cancel;
    private EditText ed_position;
    private SuggestionSearch mSuggestionSearch = SuggestionSearch.newInstance();
    private PositionAdapter adapter;
    private final ArrayList<SuggestionResult.SuggestionInfo> datas = new ArrayList<>();
    String city = "北京";
    private ImageView img_deletedt;

    @Override
    protected void initListener() {
        tx_cancel.setOnClickListener(view -> finish());
        tx_search_cancel.setOnClickListener(view -> ed_position.setText(""));
        ed_position.setOnKeyListener(onKey);
        ed_position.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {
            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                if (s.toString().isEmpty()) {
                    datas.clear();
                    adapter.notifyDataSetChanged();
                    return;
                }
                mSuggestionSearch.requestSuggestion(new SuggestionSearchOption()
                        .city(city)
                        .keyword(s.toString())
                );
            }

            @Override
            public void afterTextChanged(Editable s) {
                if (ed_position != null) {
                    img_deletedt.setVisibility(View.VISIBLE);
                } else {
                    img_deletedt.setVisibility(View.GONE);
                }
            }
        });
        img_deletedt.setOnClickListener(new CustomClickListener() {
            @Override
            protected void onSingleClick() {
                ed_position.setText(null);
            }

            @Override
            protected void onFastClick() {

            }
        });
        mSuggestionSearch.setOnGetSuggestionResultListener(suggestionResult -> {
            datas.clear();
            if (suggestionResult != null && suggestionResult.getAllSuggestions() != null) {
                for (SuggestionResult.SuggestionInfo allSuggestion : suggestionResult.getAllSuggestions()) {
                    if(allSuggestion.address.isEmpty()){
                        continue;
                    }
                    datas.add(allSuggestion);
                }
            }
            Collections.sort(datas,(l1,l2) -> l1.getCity().compareTo(l2.getCity()));
            adapter.notifyDataSetChanged();
        });
        adapter.setOnItemClickListener((adapter, view, position) -> {
            this.adapter.setIndexPosition(position);
            adapter.notifyDataSetChanged();
        });
        textComplete.setOnClickListener(v -> {
            Intent intent = new Intent();
            if (adapter.getIndexPosition() != null && datas.size() > adapter.getIndexPosition()) {
                intent.putExtra("suggestionInfo", datas.get(adapter.getIndexPosition()));
            }
            setResult(Activity.RESULT_OK, intent);
            finish();
        });
        if (MyAppliaction.getMyAppliaction().bdLocation != null) {
            if (MyAppliaction.getMyAppliaction().bdLocation.getAddress() != null) {
                if (MyAppliaction.getMyAppliaction().bdLocation.getAddress().address != null) {
                    mSuggestionSearch.requestSuggestion(new SuggestionSearchOption()
                            .city(city)
                            .keyword(MyAppliaction.getMyAppliaction().bdLocation.getAddress().address)
                    );
                }
            }
        }


    }

    View.OnKeyListener onKey = (v, keyCode, event) -> {
//             TODO Auto-generated method stub
        if (keyCode == KeyEvent.KEYCODE_ENTER) {
            InputMethodManager imm = (InputMethodManager) v.getContext().getSystemService(Context.INPUT_METHOD_SERVICE);
            if (imm.isActive()) {
                imm.hideSoftInputFromWindow(v.getApplicationWindowToken(), 0);
            }
            return true;
        }
        return false;
    };

    @Override
    protected void initLayout(Bundle savedInstanceState) {
        setContentView(R.layout.activity_position);
        setStatusBarTranslucent();
    }

    @Override
    protected void initView() {
        tx_cancel = findViewById(R.id.tx_cancel);
        textComplete = findViewById(R.id.textComplete);
        tx_search_cancel = findViewById(R.id.tx_search_cancel);
        ed_position = findViewById(R.id.ed_position);
        RecyclerView recyclerView = findViewById(R.id.recyclerView);
        img_deletedt = findViewById(R.id.img_deletedt);
        adapter = new PositionAdapter(datas);
        recyclerView.setAdapter(adapter);
        recyclerView.setLayoutManager(new LinearLayoutManager(this));

        if (MyAppliaction.getMyAppliaction().bdLocation != null && MyAppliaction.getMyAppliaction().bdLocation.getCity() != null) {
            city = MyAppliaction.getMyAppliaction().bdLocation.getCity();
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        mSuggestionSearch.destroy();
    }
}
